package com.biz.primus.management.promotion.tag;

import com.biz.primus.model.promotion.enums.PromotionScopeEnum;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 促销范围选择标签(单选)
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class PromotionScopeSelectTag extends TagSupport {
    private static final long serialVersionUID = -3109542889098213086L;

    /**
     * 已选择范围
     */
    private String selectScope;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        try {
            for (PromotionScopeEnum scopeEnum : PromotionScopeEnum.values()) {
                if (StringUtils.equalsIgnoreCase(selectScope, scopeEnum.name())) {
                    jspWriter.print(String.format("<option value=\"%s\" selected >%s</option>", scopeEnum.name(), scopeEnum.getDesc()));
                } else {
                    jspWriter.print(String.format("<option value=\"%s\">%s</option>", scopeEnum.name(), scopeEnum.getDesc()));
                }
            }
        } catch (IOException e) {
            log.error("输出促销范围选择标签内容失败", e);
        }
        return super.doStartTag();
    }
}
